<template>
  <div class="box">
    <img src="https://m.xiaomiyoupin.com/youpin/static/m/res/images/icons/icon_arrow_left_circle_gray.png" alt="" @click="$router.go(-1)" class="back">
    <div class="content">
      <h4>欢迎登录小米有品</h4>
      <div class="form">
        <van-form validate-first @failed="onFailed" @submit="onSubmit">
            <!-- 验证手机号 -->
            <van-field
              v-model="tel"
              name="pattern"
              clearable
              placeholder="邮箱/手机号码/小米ID"
              :rules="[{ required: true, pattern: pattern1, message: '请输入正确手机号码' }]"
            />
            <!-- 验证密码 -->
            <van-field
              v-model="password"
              name="password"
              type="password"
              clearable
              placeholder="请输入密码"
              :rules="[{ required: true,pattern: pattern2, message: '密码长度不能少于6位' }]"
            />
            <div style="margin: 16px;">
              <van-button round block type="info" native-type="submit">
                登录
              </van-button>
              <van-button round block to="/register">
                去注册
              </van-button>
          </div>
        </van-form>
      </div>
    </div>
    <p>用户名密码登录</p>
    <h5>收不到验证码？</h5>
    <van-divider>其他方式登录</van-divider>
  </div>
</template>

<script>
import Vue from 'vue'
import { Button, NavBar, Form, Field, Toast, Divider } from 'vant'
// import { login } from '@/api'
Vue.use(Button)
Vue.use(NavBar)
Vue.use(Form)
Vue.use(Field)
Vue.use(Toast)
Vue.use(Divider)
export default {
  data () {
    return {
      tel: '',
      password: '',
      pattern1: /^1[3456789]\d{9}$/,
      pattern2: /\d{6}/
    }
  },
  methods: {
    onFailed (errorInfo) {
      console.log('failed', errorInfo)
      Toast.fail(errorInfo.errors[0].message)
    },
    onSubmit () {
      console.log(this.$store)
      this.$store.dispatch('users/login', {
        tel: this.tel,
        password: this.password
      }).then((data) => {
        console.log(this.$store.state.users)
        const code = this.$store.state.users.login_code
        if (code === '10400') {
          Toast('信息填写不完整')
        } else if (code === '10808') {
          Toast('该用户为注册')
        } else {
          Toast('登陆成功')
          localStorage.setItem('userid', data.userid)
          localStorage.setItem('token', data.token)
          localStorage.setItem('nickname', data.nickname)
          localStorage.setItem('loginState', 'ok')
          this.$router.go(-1)
        }
      })
      // console.log('登录')
      // login({
      //   tel: this.tel,
      //   password: this.password
      // }).then(res => {
      //   console.log(res.data)
      //   const { code } = res.data
      //   switch (code) {
      //     case '10400':
      //       Toast('信息填写不完整')
      //       break
      //     case '10888':
      //       Toast('登陆成功')
      //       this.$router.replace('/login')
      //       localStorage.setItem('userid', res.data.data.userid)
      //       localStorage.setItem('token', res.data.data.token)
      //       localStorage.setItem('nickname', res.data.data.nickname)
      //       // 给本地保存一个登录状态，用于前端校验
      //       // 加入购物车，获取本地loginState，如果未ok，进入服务器校验，如果不存在，不走服务器，直接进入登陆
      //       localStorage.setItem('loginState', 'ok')
      //       this.$router.go(-1)
      //       break
      //     case '10808':
      //       Toast('该用户已注册')
      //       break
      //     default:
      //       break
      //   }
      // })
    }
  }
}
</script>
<style lang="scss" scoped>
@import '@/lib/reset.scss';
// 首页flex：1影响了登录注册页
.container .box .content {
  flex: none;
}
.box {
  background: url(https://shop.io.mi-img.com/app/shop/img?id=shop_dfa89d9b977aa2f32346a1e2e3ecb838.png)no-repeat;
  background-size: 100% auto ;
  padding: 60px 46px;
  .van-button {
    background-color: #E4BB7E;
    border:none;
    color: white;
    margin-top: 24px;
    height: 40px;
    // width: 320px;
  }
  .form {
    margin-top: 50px;
  }
}
h4 {
  font-size: 0.24rem;
  margin: 16px 0;
  text-align: center;
  color: #666;
  font-weight: normal;
}
h5{
  text-align: center;
  margin-top: 30px;
  margin-bottom: 50px;
  font-size: .16rem;
  color: #9b9b9b;
  font-weight: 100;
}
h6{
  text-align: center;
  font-weight: 100;
}
p{
  padding: 20px;
}
.back {
  width:50px;
  position:absolute;
  top:0px;
  left:0px;
}
</style>
